const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popupConsignmentShow: false,
    popupPlatformShow: false,
    takeGoodsShow: false,
    name: "",
    goods_name: "",
    page: 1,
    finished: false,
    listData:[],
    lock: "",
    manage_id:"", //我的存货列表的ID
    manage_goods:{}, //提货-弹窗数据
    timeData: {},
    now: "",
    order_id:"",
    is_sign: false, //为ture 请求电子合同
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.name,
    });
    this.setData({
      name: options.name,
      now: String(Date.parse(new Date())).substring(0, 10),
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getData();
  },
  getData () {
    if (this.data.finished) return;
    let url = this.data.name == '代销管理' ? "plugin.stock-service.frontend.controllers.help.index" : this.data.name == '我的存货' ? "plugin.stock-service.frontend.controllers.stock.index" : '';
    let baseUrl = app.getNetAddresss(url);
    app._getNetWork({
      url: baseUrl,
      data: {
        page: this.data.page,
        goods_name:this.data.goods_name
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let listData = [];
        this.setData({
          lock: res.lock,
          is_sign: res.is_sign
        });
        if (res.list.data.length === 0 || res.list.data.length < res.list.per_page || res.list.current_page === res.list.last_page) {
          finished = true;
        }
        listData = this.data.listData.concat(res.list.data);
        // for(let [index,item] of listData.entries()) {
        //   item.lock_time = 1670488219 - index * 10;
        // }
        this.setData({
          finished,
          listData,
          page: ++this.data.page,
        });
      }
    });
  },
  // 确认代销
  async sureConsignment() {
    if(this.data.is_sign) {
     //电子合同
     app._getNetWork({
      url: app.getNetAddresss('plugin.shop-esign.frontend.contract.get-contract'),
      data: {
        activity_id: this.data.order_id, 
        plugin_name: "stock-service"
      },
      success: (res)=> {
        var _data = res.data;
        if (res.data.result == 1) {
          if (_data.data.need_sign === 1 && _data.data.contract_id) {
            wx.showModal({
              title: '提示',
              content: `代销前需要签署电子合同`,
              showCancel: false,
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: `/packageH/signPage/signPage?id=${_data.data.contract_id}&fromOrder=1`,
                  });
                }
              }
            })
          }else{
            this.asyncConsignment();
          }
        }
      },
    });
    }else {
      this.asyncConsignment();
    }
  },
  asyncConsignment() {
    let baseUrl = app.getNetAddresss("plugin.stock-service.frontend.controllers.stock.help");
    app._getNetWork({
      url: baseUrl,
      data: {
        manage_id: this.data.manage_id,
      },
      success: (respone) => {
        let { data, msg, result } = respone.data;
        wx.showToast({
          title: msg,
          icon: "none"
        });
        if(result) {
          this.setData({
            popupConsignmentShow: false,
            page: 1,
            finished: false,
            listData:[],
          })
          this.getData();
        }
      },
    });
  },
  // 提货
  openTakeGoodsShow(e) {
    this.setData({
      takeGoodsShow: true,
      manage_goods: e.currentTarget.dataset.item
    })
  },
  // 代销
  openConsignment(e) {
    if(!e.currentTarget.dataset.item.help_button) return
    this.setData({
      popupConsignmentShow: true,
      manage_id: e.currentTarget.dataset.item.id,
      order_id: e.currentTarget.dataset.item.order_id
    })
  },
  // 关闭代销
  closePopupConsignmentShow() {
    this.setData({
      popupConsignmentShow: false,
    })
  },
  // 平台回购
  clickPopupPlatformShow(e) {
    if(!e.currentTarget.dataset.item.back_button) return
    this.setData({
      popupPlatformShow: true,
      manage_id: e.currentTarget.dataset.item.id
    })
  },
  // 关闭平台回购
  closePopupPlatformShow() {
    this.setData({
      popupPlatformShow: false
    })
  },
   // 确定平台回购
  surePlatform() {
    let baseUrl = app.getNetAddresss("plugin.stock-service.frontend.controllers.help.back");
    app._getNetWork({
      url: baseUrl,
      data: {
        manage_id: this.data.manage_id,
      },
      success: (respone) => {
        let { data, msg, result } = respone.data;
        wx.showToast({
          title: msg,
          icon: "none"
        });
        if(result) {
          this.setData({
            popupPlatformShow: false,
            page: 1,
            finished: false,
            listData:[],
          });
          this.getData();
        }
      },
    });
  },
  // 搜索
  onSearch(e) {
    this.setData({
      goods_name: e.detail,
      page: 1,
      finished: false,
      listData:[],
    });
    this.getData();
  },
  finishTime(e) {
    // let time = String(Date.parse(new Date())) * 1 / 1000
    // this.setData({
    //   ['listData[' + e.currentTarget.dataset.index + '].take_button']:this.data.listData[e.currentTarget.dataset.index].lock_time <= time ? true : this.data.listData[e.currentTarget.dataset.index].take_button,
    //   ['listData[' + e.currentTarget.dataset.index + '].back_button']:this.data.listData[e.currentTarget.dataset.index].lock_time <= time ? true : this.data.listData[e.currentTarget.dataset.index].back_button
    // });
  }
})